#include "RazoukRampalLerenduFSM_standard_machine.h"

using namespace FSM;

FSM_standard_machine::FSM_standard_machine (const FSM_machine &machine) : FSM_machine(machine)
{
	list <FSM_State*>::iterator it1;
	list <FSM_transition*>::iterator it2;

	if(_initialStates.size() == 1)
	{
		bool transitionFound = false;
		for(it1 = _states.begin(); it1 != _states.end(); ++it1)
			for(it2 = (*it1)->transitions.begin(); it2 != (*it1)->transitions.end(); it2++)
				if((*it2)->nextState == _initialStates.front())
					transitionFound = true;

		if(!transitionFound)
		{
			cout << "L'automate est deja standard. Retour." << endl;
			return;
		}
	}

	FSM_State* newState = new FSM_State;
	newState->initial = true;
	newState->final = false;
	newState->name = "Initial_for_stand";


	for(it1 = _initialStates.begin(); it1 != this->_initialStates.end(); it1++)
	{
		for(it2 = (*it1)->transitions.begin(); it2 != (*it1)->transitions.end(); it2++)
			newState->transitions.push_back(*it2);

		if((*it1)->final == true)
			newState->final = true;

		(*it1)->initial = false;
	}

	_initialStates.clear();
	_initialStates.push_back(newState);
	_states.push_front(newState);
}